const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');
const Note = require('./Note');

const NoteTag = sequelize.define('NoteTag', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true
  },
  note_id: {
    type: DataTypes.STRING(255),
    allowNull: false,
    references: {
      model: Note,
      key: 'id'
    }
  },
  tag: {
    type: DataTypes.STRING(50),
    allowNull: false
  }
}, {
  tableName: 'note_tags',
  timestamps: false,
  indexes: [
    {
      unique: true,
      fields: ['note_id', 'tag']
    }
  ]
});

// 关联
NoteTag.belongsTo(Note, { foreignKey: 'note_id', onDelete: 'CASCADE' });

module.exports = NoteTag;